package com.itic.mobile.crawler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

/*
 * 登录后爬取
 * Refer: https://nutcher.org/topics/33
 * https://github.com/CrawlScript/WebCollector/blob/master/README.zh-cn.md
 * Lib required: webcollector-2.07-bin, selenium-java-2.44.0 & its lib
 */
public class CrawlerInterface {
     
	 /**
     * 模拟csdn登录，获取登录后的cookies保存到文件
     * 
     * @param username
     *            用户名
     * @param password
     *            密码
     * @param geckodriverpath
     *            gecko的路径（见https://github.com/mozilla/geckodriver）
     * @param savecookiepath
     *            cookies保存的路径
     * @throws Exception
     */
    public static void firfoxDriverGetCookies(String username, String password, String geckodriverpath,
            String savecookiepath) throws Exception {
        // 初始化参数据
        System.setProperty("webdriver.gecko.driver", geckodriverpath);
        FirefoxDriver driver = new FirefoxDriver();
        
        //String baseUrl = "https://media.jd.com/";
        
        String baseUrl = "https://login.taobao.com/member/login.jhtml";
        // 加载url
        driver.get(baseUrl);
        // 等待加载完成
        Thread.sleep(3000);
        // 获取页面元素
        WebElement elemUsername =  driver.findElementById("TPL_username_1");
        WebElement elemPassword =  driver.findElementById("TPL_password_1");
        WebElement btn = driver.findElementById("J_SubmitStatic");
        // 操作页面元素
        elemUsername.sendKeys(username);
        elemPassword.sendKeys(password);
        // 提交表单
        btn.submit();
        Thread.sleep(5000);
        driver.get("http://download.csdn.net/");
        Thread.sleep(5000);
        // 获取cookies
        driver.manage().getCookies();
        Set<org.openqa.selenium.Cookie> cookies = driver.manage().getCookies();
        System.out.println("Size: " + cookies.size());
        Iterator<org.openqa.selenium.Cookie> itr = cookies.iterator();

        CookieStore cookieStore = new BasicCookieStore();

        while (itr.hasNext()) {
            Cookie cookie = itr.next();
            BasicClientCookie bcco = new BasicClientCookie(cookie.getName(), cookie.getValue());
            bcco.setDomain(cookie.getDomain());
            bcco.setPath(cookie.getPath());
            cookieStore.addCookie(bcco);
        }
        // 保存到文件
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(savecookiepath)));
        oos.writeObject(cookieStore);
        oos.close();

    }

    
    public static void main(String[] args) throws Exception {

        firfoxDriverGetCookies("18401762162", "nizuibang521", "C://Users/Administrator/Desktop/geckodriver.exe", "C://Users/Administrator/Desktop/pac/cookie.ser");
    }
 
}